package diona.emotions.kinopoisk

import util.matching.Regex
import io.Source
import util.matching.Regex.MatchIterator
import org.apache.commons.lang.StringEscapeUtils
import diona.emotions.TextUtil

/**
 * User: anton
 * Date: 20.06.11 23:31
 */
private object Parser
{
  def parse(): (List[String], List[String]) = parse("pos") -> parse("neg")

  private def parse(sentiment: String): List[String] =
  {
    val text = Source.fromInputStream(
      this.getClass.getResourceAsStream(String.format("/emotions/kinopoisk/%s.html", sentiment)), "windows-1251")
      .mkString
    val iter: MatchIterator = new Regex("""(?s)id="ext_text_.+?">.*?<span.+?>(.*?)</span>""").findAllIn(text)

    iter.matchData.map(m => clearText(m.group(1))).toList
  }

  private def clearText(text: String): String =
  {
    TextUtil.clearText(StringEscapeUtils.unescapeHtml(text.replaceAll("<.*?>", " ")))
  }
}